package com.biwano.swapmyjob.beans.user;


import javax.ejb.EJB;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.biwano.swapmyjob.beans.ValidatorBean;
import com.biwano.swapmyjob.dao.UserDao;
import com.biwano.swapmyjob.objects.User;


@ManagedBean
@ViewScoped
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class RegisterBean extends ValidatorBean  {

	User user;

	private String passwordconfirmation;

	@EJB
	UserDao userDao;

	@Override
	public void validateInput() {
		checkMandatory(user.getLogin(), "login");
		checkAvailable(userDao, user.getLogin(), "login");
		checkEmail(user.getEmail(), "email");
		checkAvailable(userDao, "email", user.getEmail(), "email");
		checkMandatory(user.getPassword(), "password");
		checkEquals(user.getPassword(), getPasswordconfirmation(), "password2");
	}

	@Override
	public RETURNVALUE beanAction() {
		userDao.register(user);
		return RETURNVALUE.registersuccess;
	}



	public User getUser() {
		if (user==null) user = new User();
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getPasswordconfirmation() {
		return passwordconfirmation;
	}

	public void setPasswordconfirmation(String passwordconfirmation) {
		this.passwordconfirmation = passwordconfirmation;
	}

	@Override
	public String getFormId() {
		return "register";
	}


}
